<?php
namespace app\index;

use coolq\CoolQ; 
use think\Controller;
use think\Config;
/*
  控制器基类
*/
class Base extends Controller
{
    public $CQ; # 封装类对象 直接复制成员属性
    public $CQDATA; #php端接受到的数据

    public function __construct(){
      parent::__construct();
      $url = Config::get('url'); 
      $port = Config::get('port');
      if($port!=80 && $url) $url .= ':'.$port;
      $key = Config::get('key');
      $effectTime = Config::get('effectTime');
      $format = Config::get('format');

      $CQ = new CoolQ($url,$key,$effectTime,$format);
      $this->CQ = $CQ;
      $this->CQDATA = $this->CQ->receive();
    }
    
    /**
    * TP5内置初始化函数
    */
    public function __initialize(){
      // TODO
    }

    /**
    *  通过方法获取对象
    */
    public static function CQ()
    { 
      $url = Config::get('url'); 
  		$port = Config::get('port');
  		if($port!=80 && $url) $url .= ':'.$port;
  		$key = Config::get('key');
  		$effectTime = Config::get('effectTime');
  		$format = Config::get('format');

      $CQ = new CoolQ($url,$key,$effectTime,$format);  
      return $CQ; 
    }

    /**
    * 判断cq返回类型
    */
   public function getCoolqType(){
      # php接收到消息开始工作 没有空
      $resultArray = [];
      $coolqData = $this->CQDATA;
      if(is_array($coolqData) && !empty($coolqData)){
        $resultArray = array(
            'type'=>$coolqData['type'],
            'msg'=>$coolqData['msg'],
            'data'=>array(),
        );

      }else{
        # 没有接收到消息释放CQ
        @unset($this->CQ);
      }

      return $resultArray;
   }
}
